﻿using BXJG.Common.Http;
using Microsoft.Extensions.DependencyInjection.Extensions;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Abp.Application.Navigation;
using Microsoft.Extensions.DependencyInjection;
using Abp.Configuration;
using BXJG.Utils.Application.Share.GeneralTree;
using BXJG.Utils.Application.ClientProxy.Http;
using System.Net.Http;
using BXJG.Utils.Application.ClientProxy;
using System.Net.Http.Json;
using System.Text.Json;

namespace Microsoft.Extensions.DependencyInjection
{
    public static class DIExt
    {
        /// <summary>
        /// 添加httpclient的拦截器，用于添加accessToken请求头，并在响应时解包abp ajax包
        /// </summary>
        /// <param name="services"></param>
        /// <returns></returns>
        public static IServiceCollection AddBXJGUtilsMessageHandler(this IServiceCollection services, Action<HttpClient> act = default, Action<IHttpClientBuilder> act2 = default)
        {

            IHttpClientBuilder b;

            if (act != default)
                b = services.AddHttpClient(BXJGHttpClientExt.HttpClientName, act);
            else
                b = services.AddHttpClient(BXJGHttpClientExt.HttpClientName);

            services.AddTransient<AbpWraperDelegatHandler>();
            services.AddTransient<IErrorCallback,NullErrorCallback>();
            b.AddHttpMessageHandler<AbpWraperDelegatHandler>().AddHttpMessageHandler<AccessTokenHandler>();

            if (act2 != default)
                act2(b);


            //services.ConfigureHttpClientDefaults(c=>c.json)

         

            return services;

           

        }
       
    }
}
